package service

import (
	"blog_go/internal/model"
	"blog_go/internal/repository"
	"errors"
	"mime/multipart"
)

type UserService interface {
	QueryFollowUser(userId int) ([]*model.UserView, error)
	QueryAllUser() ([]*model.UserView, error)
	QueryUser(userId int, targetUserId int) (*model.UserView, error)
	FollowUser(userId int, followerId int, isFollow bool) error
	UpdateUserStatus(userId int, status int) error
	UpdateUserProfile(user model.User, files *multipart.Form, userId int) (*model.UserView, error)
	UpdatePwd(user model.UserPwd) error
	ResetUserPwd(userId int) error
	RegisterUser(user model.User) error
	LoginUser(loginUser model.LoginUser) (*model.UserView, error)
	LoginAdmin(loginUser model.LoginUser) (*model.Admin, error)
	DelUser(userId int) error
	RefreshToken(userId int) (*model.User, error)
}

type userService struct {
	*Service
	userRepository repository.UserRepository
}

func NewUserService(service *Service, userRepository repository.UserRepository) UserService {
	return &userService{
		Service:        service,
		userRepository: userRepository,
	}
}

// 关注/取消用户
func (s *userService) FollowUser(userId int, followerId int, isFollow bool) error {
	if userId == 0 || followerId == 0 {
		return errors.New("参数错误")
	}

	if isFollow {
		return s.userRepository.FollowUser(userId, followerId)
	} else {
		return s.userRepository.UnFollowUser(userId, followerId)
	}
}

// 查询关注用户信息
func (s *userService) QueryFollowUser(userId int) ([]*model.UserView, error) {
	if userId == 0 {
		return nil, errors.New("参数错误")
	}

	return s.userRepository.QueryFollowUser(userId)
}

// 查看用户信息
func (s *userService) QueryUser(userId int, targetUserId int) (*model.UserView, error) {
	if targetUserId == 0 {
		return nil, errors.New("参数错误")
	}

	return s.userRepository.QueryUser(userId, targetUserId)
}

// 查看所有人信息
func (s *userService) QueryAllUser() ([]*model.UserView, error) {
	return s.userRepository.QueryAllUser()
}

// 修改用户的状态
func (s *userService) UpdateUserStatus(userId int, status int) error {
	if userId == 0 {
		return errors.New("参数错误")
	}

	return s.userRepository.UpdateUserStatus(userId, status)
}

// 更新用户资料
func (s *userService) UpdateUserProfile(user model.User, files *multipart.Form, userId int) (*model.UserView, error) {
	if user.UserId != userId {
		return nil, errors.New("不能修改他人资料")
	}

	if len(user.DisplayName) > 18 || len(user.DisplayName) < 3 {
		return nil, errors.New("用户名长度应该在3-8之间")
	}

	if len(user.Signature) > 255 {
		return nil, errors.New("个性签名长度不能超过255")
	}

	return s.userRepository.UpdateUserProfile(user, files)
}

// 更新用户，管理员密码
func (s *userService) UpdatePwd(user model.UserPwd) error {
	if user.UserId == 0 {
		return errors.New("参数错误")
	}

	return s.userRepository.UpdatePwd(user)
}

// 重置用户密码
func (s *userService) ResetUserPwd(userId int) error {
	if userId == 0 {
		return errors.New("参数错误")
	}

	return s.userRepository.ResetUserPwd(userId)
}

// 用户登录
func (s *userService) LoginUser(loginUser model.LoginUser) (*model.UserView, error) {
	return s.userRepository.LoginUser(loginUser)
}

// 管理员登录
func (s *userService) LoginAdmin(loginUser model.LoginUser) (*model.Admin, error) {
	return s.userRepository.LoginAdmin(loginUser)
}

// 用户注册
func (s *userService) RegisterUser(user model.User) error {
	return s.userRepository.RegisterUser(user)
}

func (s *userService) DelUser(userId int) error {
	if userId == 0 {
		return errors.New("参数错误")
	}

	return s.userRepository.DelUser(userId)
}

func (s *userService) RefreshToken(userId int) (*model.User, error) {
	return s.userRepository.RefreshToken(userId)
}
